package com.yisu.expressway.onedollar.widget;

import com.yisu.expressway.R;
import com.yisu.expressway.utils.HBLog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

/**
 * A subclass of {@link PopupWindow} to show a custom PopupWindow.
 */
@SuppressLint("ViewConstructor")
public class DownUpPopupWindow extends PopupWindow {
    protected Context mContext;
    protected View mMenuView;
    private int mMenuViewHeight;

    protected Animation showAnimation;
    protected Animation hideAnimation;
    protected long duration;

    protected Handler mHandle = new Handler();

    public DownUpPopupWindow(Activity context,View menuView) {
        super(context, null);
        mContext = context;
        initAnimation();
	    setMenuView(menuView);
    }

    private void setMenuView(View menuView) {
        if (menuView == null) {
            return;
        }

	    RelativeLayout contentLayout = new RelativeLayout(mContext);
        contentLayout.setBackgroundColor(mContext.getResources().getColor(R.color.status_view));
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        contentLayout.addView(menuView, lp);
        mMenuView = menuView;
        mMenuView.post(new Runnable() {
            @Override
            public void run() {
                mMenuViewHeight = mMenuView.getHeight();
            }
        });
        contentLayout.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuViewHeight;
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
        this.setContentView(contentLayout);
        this.setWidth(LayoutParams.MATCH_PARENT);
        this.setHeight(LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x50000000);
        this.setBackgroundDrawable(dw);
	    this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }

    @Override
    public void dismiss() {
        if (null != hideAnimation) {
            mMenuView.startAnimation(hideAnimation);

            mHandle.postDelayed(new Runnable() {
                @Override
                public void run() {
                    dismissPopupWindow();
                }
            }, duration);
        } else {
            dismissPopupWindow();
        }
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if (null != showAnimation && mMenuView != null) {
            mMenuView.startAnimation(showAnimation);
        }
        super.showAsDropDown(anchor, xoff, yoff);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        if (null != showAnimation && mMenuView != null) {
            mMenuView.startAnimation(showAnimation);
        }
        super.showAtLocation(parent, gravity, x, y);
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (null != showAnimation && mMenuView != null) {
            mMenuView.startAnimation(showAnimation);
        }
        super.showAsDropDown(anchor);
    }

    private void initAnimation() {
        showAnimation = AnimationUtils.loadAnimation(mContext, R.anim.popup_show);
        hideAnimation = AnimationUtils.loadAnimation(mContext, R.anim.popup_hide);
        duration = hideAnimation.getDuration();
    }

    private void dismissPopupWindow() {
        try {
            super.dismiss();
        } catch (Exception e) {
        }
    }
}
